#include "rat.h"

fraction::fraction ( int a, int b )
{ 
	up = a;
	down = b;
	if (down < 0)
		up = -up;
	simplify();
}

fraction::fraction ( int a )
{
	up = a;
	down = 1;
}

fraction::fraction ( const fraction& t )
{
	up = t.up;
	down = t.down;
}


void fraction::print ( ) const
{
	std::cout << up << "/" << down << std::endl;
}


fraction& fraction::operator += ( const fraction& f1 )  // method   __reference
{	
	up = up * f1.down + f1.up * down;
	down = down * f1.down;
	simplify();
	return *this;
}
	

fraction& fraction::operator -= ( const fraction& f1 )  // method   __reference
{	
	up = up * f1.down - f1.up * down;
	down = down * f1.down;
	simplify();
	return *this;
}
	
fraction& fraction::operator /= ( const fraction& f1 )  // method   __reference
{	
	up = up * f1.down;
	down = down * f1.up;	
	simplify();	
	return *this;
}
	
fraction& fraction::operator *= ( const fraction& f1 )  // method   __reference
{	
	up = up * f1.up;
	down = down * f1.down;		
	simplify();
	return *this;
}
	
	
fraction& fraction::operator ++ ()
{
	up = up + down;
	simplify();
	return *this;			
}
	
fraction fraction::operator ++ (int) 
{
	fraction tmp (*this);
	up = up + down;
	simplify();
	return tmp;				
}

	
fraction& fraction::operator -- ()
{
	up = up - down;
	simplify();
	return *this;			
}
	
fraction fraction::operator -- (int) 
{
	fraction tmp (*this);
	up = up - down;
	simplify();
	return tmp;				
}
		 
fraction::operator double()
{
	double p = up / down;
	return p;	
}
	
fraction fraction::operator- ()
{
	fraction tmp ( *this );
	tmp.up = -tmp.up;
	return tmp;
} 
	
void fraction::simplify ()
{
	int j;
	while ( ( j = GCD ( up , down ) ) != 1 )
	{
		up = up / j;
		down = down / j;
	}
}

int fraction::GCD ( int a , int b )
{
	while ( a != 0 && b != 0 )
       {
		if ( a >= b ) a = a % b;
           else b = b % a;
       }
       return a + b;
}

bool operator > ( const fraction& th , const fraction& t )
{
	if ( th.up * t.down > t.up * th.down ) 
		return true;
	return false;
}
	

bool operator < ( const fraction& th , const fraction& t )
{
	if ( th.up * t.down < t.up * th.down ) 
		return true;
	return false;
}



bool operator == ( const fraction& th , const fraction& t )
	{
		if (( th.up == t.up ) && ( th.down == t.down ))
			return true;
		return false;
	}



bool operator >= ( const fraction& th , const fraction& t )
{
	if (( th > t ) || ( th == t ))
		return true;
	return false;
}

bool operator <= ( const fraction& th , const fraction& t )
{
	if (( th < t ) || ( th == t ))
		return true;
	return false;
}



bool operator != ( const fraction& th , const fraction& t )
{
	if (( th.up != t.up ) || ( th.down != t.down ))
		return true;
	return false;
}


fraction operator + ( const fraction& f1, const fraction& f2)  // function
{
	fraction tmp(f1);
	tmp+=f2;
	return tmp;
}

fraction operator - ( const fraction& f1, const fraction& f2)  // function
{
	fraction tmp(f1);
	tmp-=f2;
	return tmp;
}

fraction operator * ( const fraction& f1, const fraction& f2)  // function
{
	fraction tmp(f1);
	tmp*=f2;
	return tmp;
}

fraction operator / ( const fraction& f1, const fraction& f2)  // function
{
	fraction tmp(f1);
	tmp/=f2;
	return tmp;
}
